<!--
* @author wn
* @date 2023/08/09 11:11:59
* @description: 热门推荐
!-->
<template>
	<!-- 推荐封面图 -->
	<view class="cover">
		<image :src="bannerPicture" mode="scaleToFill"></image>
	</view>
	<!-- 推荐选项 -->
	<view class="tabs">
		<text
			v-for="(item, index) in subTypes"
			:key="item.id"
			class="text"
			:class="{ active: activeIndex === index }"
			@tap="activeIndex = index"
		>
			{{ item.title }}
		</text>
	</view>
	<!-- 推荐列表 -->
	<scroll-view
		enable-back-to-top
		v-for="(item, index) in subTypes"
		:key="item.id"
		v-show="activeIndex === index"
		scroll-y
		class="scroll-view"
		@scrolltolower="getMore"
	>
		<view class="flow">
			<NavigatorGoods
				v-for="good in item.goodsItems.items"
				:key="good.id"
				:good="good"
			/>
		</view>
		<view class="loading-text">
			{{ item.isFinish ? '没有更多数据了~' : '正在加载...' }}
		</view>
	</scroll-view>
</template>
<script lang="ts" setup>
import { getHotResultApi } from '@/api/hot'
import { reactive, ref, watch } from 'vue'

// uniapp 获取页面参数 内部封装在 props 中
const { type } = defineProps<{ type: string }>()

// 热门推荐页 类型 标题 url
const contentMap = [
	{ type: '1', title: '特惠推荐', url: '/hot/preference' },
	{ type: '2', title: '爆款推荐', url: '/hot/inVogue' },
	{ type: '3', title: '一站买全', url: '/hot/oneStop' },
	{ type: '4', title: '新鲜好物', url: '/hot/new' },
]

// 根据类型 获取当前 内容
const currentContent = contentMap.find(v => v.type === type)

// 动态设置标题
currentContent && uni.setNavigationBarTitle({ title: currentContent.title })

// 推荐封面图
const bannerPicture = ref('')
// 高亮的下标
const activeIndex = ref(0)

/**
 * 推荐选项--直接根据 index 取出对应 subType
 * 每一个 SubType 都应该有自己 独立的 isFinish 用来判断是都全部加载完毕
 */
const subTypes = ref<(SubType & { isFinish?: boolean })[]>([])

/**
 * 分页参数
 * 首次加载不传递 subType 获取 tabs 所有 数据
 * 之后根据 传递 subType  获取 tab 对应 数据
 *
 * tab切换 不重新加载 不 resetData  列表不刷新
 */
const hotParams = reactive<HotParams>({ page: 1, pageSize: 10 })

// 首次加载数据标识
let isFirst = true
// watch hotParams -- 获取热门推荐数据
watch(
	hotParams,
	async newValue => {
		const { bannerPicture: picture, subTypes: subs } = await getHotResultApi(
			currentContent!.url,
			newValue
		)

		bannerPicture.value = picture

		if (isFirst) {
			isFirst = false
			subTypes.value = subs
			/**
			 * 首次加载 判断  isFinish  只有一页数据 或者没有数据
			 * pages<=1 isFinish=true
			 */
		} else {
			const activeSubTypes = subTypes.value[activeIndex.value]
			const beforeList = activeSubTypes.goodsItems.items
			Object.assign(activeSubTypes, subs[activeIndex.value])

			activeSubTypes.goodsItems.items.unshift(...beforeList)
		}
	},
	{ immediate: true }
)

// 上拉加载更多
const getMore = () => {
	// 获取当前选项
	const activeSubTypes = subTypes.value[activeIndex.value]
	// 分页条件
	if (activeSubTypes.goodsItems.page >= activeSubTypes.goodsItems.pages) {
		activeSubTypes.isFinish = true
		return uni.showToast({ icon: 'none', title: '没有更多数据~' })
	}
	activeSubTypes.isFinish = false
	// 跟随当前 tab
	hotParams.page = activeSubTypes.goodsItems.page + 1
	// 传递 subType  获取 tab 获取对应 数据
	hotParams.subType = activeSubTypes.id
}
</script>
<style lang="scss">
page {
	display: flex;
	flex-direction: column;
}
.cover {
	height: 225rpx;
	border-radius: 0 0 40rpx 40rpx;
	overflow: hidden;
}

.tabs {
	display: flex;
	justify-content: space-evenly;
	height: 100rpx;
	margin: 0 20rpx;
	font-size: 28rpx;
	border-radius: 10rpx;
	box-shadow: 0 4rpx 5rpx rgba(200, 200, 200, 0.3);
	color: #333;
	background-color: #fff;
	position: relative;
	transform: translateY(-46%);
	.text {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		margin: 0 20rpx;
	}
	.active {
		&::after {
			content: '';
			width: 40rpx;
			height: 4rpx;
			background-color: #27ba9b;
			margin-top: 12rpx;
		}
	}
}
.scroll-view {
	// transform: translateY(-46rpx);
	margin-top: -46rpx;
}
.flow {
	display: flex;
	flex-wrap: wrap;
	justify-content: space-between;
	margin: 20rpx 20rpx 0;
	.goods {
		.name {
			padding: 0 10rpx;
		}
		.price {
			padding: 0 10rpx;
			font-size: 30rpx;
		}
	}
}

.loading-text {
	text-align: center;
	font-size: 28rpx;
	color: #666;
	padding: 20rpx 0 50rpx;
}
</style>
